package net.mingsoft.message_center.biz.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.mingsoft.basic.core.SessionContext;
import net.mingsoft.basic.dao.IModelDao;
import net.mingsoft.basic.entity.ModelEntity;
import net.mingsoft.basic.util.SqlUtil;
import net.mingsoft.message_center.action.q.MessageListQuery;
import net.mingsoft.message_center.action.r.MessageResponse;
import net.mingsoft.message_center.biz.MessageBiz;
import net.mingsoft.message_center.dao.MessageCenterDAO;
import net.mingsoft.message_center.entity.MessageCenterEntity;
import net.mingsoft.message_center.enums.MessageType;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author gengfa
 * @date 22/02/07
 */
@Service
public class MessageBizImpl implements MessageBiz {

    @Resource
    MessageCenterDAO messageCenterDAO;

    @Resource
    IModelDao modelDao;

    @Override
    public boolean add(MessageCenterEntity entity) {
        return 1 == messageCenterDAO.insertOne(entity);
    }

    @Override
    public Page<MessageResponse> list(MessageListQuery query) {
        boolean isReportExaminedRole = false;
        List<ModelEntity> modelEntities = modelDao.selectReportExamineModelByRoleId(SessionContext.session().getRoleId());
        if (CollectionUtils.size(modelEntities) == 2) {
            isReportExaminedRole = true;
        }
        query.setFileName(SqlUtil.likeStr(query.getFileName()));
        query.setOperateName(SqlUtil.likeStr(query.getOperateName()));
        Page<MessageResponse> page = messageCenterDAO.selectList(
                new Page<>(query.getPageNo(), query.getPageSize()), query, SessionContext.session().getId(), isReportExaminedRole);
        page.getRecords().forEach(it -> {
            it.setMessageType(MessageType.getDesc(it.getMessageType()));
        });
        return page;
    }

    @Override
    public List<MessageResponse> list(long userId) {
        return messageCenterDAO.selectListByUserId(userId);
    }
}
